<?php
  class BANCOS
	  {
		private $link;
    	private $query;
		private $result;
		private $user;
		private $dbname;
		private $hostname;
		public $retorno;
		public $num_registros;
		public $num_colunas;
		public $nome_colunas;
		public $tipo_banco;
		public $msn_sistema;
		public $query_result;
				
		public function __construct($banco=1,$user="postgres",$dbname="tabmoliv",$hostname="localhost",$password="f3rr31r4")
			{
			$this->tipo_banco = $banco;
			$this->user=$user;
			$this->dbname=$dbname;
			$this->hostname=$hostname;
			$this->query_result = 1;
			switch ($this->tipo_banco)
			  {
				case 1:
				  {
					$this->link = pg_connect("user=".$this->user." dbname=".$this->dbname." host=".$this->hostname." password=".$password);
					if (!$this->link)
						{
						$this->query_result = 3;
						$this->msg_sistema = pg_result_error($this->link);
						}
					//echo ("Conectado ao Banco $this->tipo_banco");
					break;
					}
				case 2:
				  {
					$this->link = mysql_connect($this->hostname,$this->user,$password);
					if (!$this->link)
						{
						$this->query_result = 3;
						$this->msg_sistema = mysql_error();
						}
					mysql_select_db($this->dbname);
					break;
					}
				case 3:
					{
					$caminho = $this->hostname.":".$this->dbname;
					$this->link = ibase_connect($caminho,$this->user,$password);
					if (!$this->link)
						{
						$this->query_result = 3;
						$this->msg_sistema = ibase_errmsg();
						}
					break;
					}
				case 4:
					{
					$this->link = odbc_connect($this->hostname, $this->user, $password);
					if (!$this->link)
						{
						$this->query_result = 3;
						$this->msg_sistema = "Erro: ".odbc_errormsg($this->link);
						}
					else 
						{
						$this->query_result = 4;
						$this->msg_sistema = "Conectado com Sucesso!";
						}
					break;
					}
				}
			}
		public function exec_query($query_ex)
		   {
			$this->query = $query_ex;
			$this->retorno = array();
//			echo ($this->query);
//			echo ($this->tipo_banco);
			switch ($this->tipo_banco)
			  	{
				case 1:
				  	{
					$this->result = pg_query($this->link,$this->query);
					if ($this->result)
					  	{
					  	$this->num_registros = pg_num_rows($this->result);
					  	if ($this->num_registros == 0)
					  		{
					  		$this->retorno[0] = true;
					  		$this->query_result = 1;
					  		}
					  	else 
					  		{
					  		$this->query_result = 2;
							$this->retorno[0][0] = true;
							$this->retorno[0][1] = $this->num_registros;
							$this->num_colunas = pg_num_fields($this->result);
							$x = 0;
							for ($i=0; $i < $this->num_registros; $i++)
							  	{
								$this->retorno[1][$x] = pg_fetch_row($this->result,$i);
								$x++;
								}
							}
						}
					else
					  	{
					  	$this->query_result = 3;
						$this->retorno[0] = false;
						$this->msg_sistema = pg_result_error($this->link);
						}
					break;
					}
				case 2:
				  {
					$this->result = mysql_query($this->query,$this->link);
					if ($this->result)
					  	{
					  	$this->num_registros = mysql_num_rows($this->result);
						$this->retorno[0][0] = true;
							if ($this->num_registros == 0)
					  		{
					  		$this->retorno[0] = true;
					  		$this->query_result = 1;
					  		}
					  	else 
					  		{
					  		$this->query_result = 2;
							$this->retorno[0][1] = $this->num_registros;
							$this->num_colunas = mysql_num_fields($this->result);
							$x=0;
							for ($i=0; $i < $this->num_registros; $i++)
							  	{
								$this->retorno[1][$x] = mysql_fetch_row($this->result);
								$x++;
								}
							}
						}
					else
					  	{
					  	$this->query_result = 3;
						$this->retorno[0] = false;
						$this->msg_sistema = mysql_error();
						}
					break;
					}
				case 3:
					{
					$this->result = ibase_query($this->link,$this->query);
					if ($this->result)
						{
						$this->num_colunas = ibase_num_fields($this->result);
						if ($this->num_colunas == 0)
					  		{
					  		$this->retorno[0] = true;
					  		$this->query_result = 1;
					  		}
					  	else 
					  		{
					  		$this->query_result = 2;
							$this->retorno[0][0] = true;
							$x = 0;
							while ($linha = ibase_fetch_row($this->result))
								{
								$this->retorno[1][$x] = $linha;
								$x++;
								}
							$this->retorno[0][1] = $x;
							$this->num_registros = $x;
							}
						ibase_free_result($this->result);
						}
					else 
						{
						$this->retorno[0] = false;
						$this->msg_sistema = ibase_errmsg();
						}
					break;
					}
				case 4:
					{
					$this->result = odbc_exec($this->link,$this->query);
					if ($this->result)
						{
						$this->num_colunas = odbc_num_fields($this->result);
						for ($c=1,$n=0; $c <= $this->num_colunas; $c++,$n++ )
							{
							$this->nome_colunas[$n] = odbc_field_name($this->result, $c);
							}
						//echo ($this->num_colunas."<br/>");
						$x = 0;
						odbc_binmode ($this->result, ODBC_BINMODE_PASSTHRU);
						odbc_longreadlen ($this->result, 0);
					  	//odbc_fetch_row($this->result, 0);
					  	ob_start(); 
						while (odbc_fetch_row($this->result))
							{
							for ($j=1,$c=0; $j <= $this->num_colunas; $j++,$c++)
								{
								$this->retorno[1][$x][$c] = odbc_result($this->result,$j);
								//echo (odbc_result_all($this->result)."<br/>");
								//echo ($this->retorno[1][$x][$c]."<br/>\n");
								}	
							//$this->retorno[1][$x] = $linha;
							$x++;
							}
						$this->num_registros = $x;
						//echo ($this->num_registros."<br/>");
						if ($this->num_registros == 0)
							{
							$this->retorno[0] = true;
					  		$this->query_result = 1;
							}
						else
							{
					  		$this->query_result = 2;
							$this->retorno[0][1] = $this->num_registros;
							}
						odbc_free_result($this->result);
						}
					else 
						{
						$this->retorno[0] = false;
						$this->msg_sistema = "Erro ao executar query ";
						}
					break;
					}
				}
			}			
		public function __destruct()
			{
			//@ini_set('display_errors','off');
			switch ($this->tipo_banco)
			  	{
				case 1:
				  	{
					pg_close($this->link);
//					echo ("Desconectado ao Banco $this->tipo_banco");
					break;
					}
				case 2:
				  	{
					mysql_close($this->link);
					break;
					}
				case 3:
					{
					ibase_close($this->link);
					break;
					}
				case 4:
					{
					odbc_close($this->link);
					break;
					}
				}
			}
		}
?>